package com.itany.corejava.day15;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

public class FileEncoryptor {

    private JFrame jf;
    private JLabel jl1;
    private JLabel jl2;
    private JCheckBox jcb;
    private JTextField jtf1;
    private JTextField jtf2;
    private JButton jb1;
    private JButton jb2;

    public FileEncoryptor() {
        jf=new JFrame("文件加密器");
        jl1=new JLabel("文件：");
        jl2=new JLabel("密钥：");
        jcb=new JCheckBox("创建新文件");
        jtf1=new JTextField(20);
        jtf2=new JTextField(5);
        jb1=new JButton("打开");
        jb2=new JButton("加密/解密");
    }

    private void init(){
        JPanel jp1=new JPanel();
        jp1.add(jl1);
        jp1.add(jtf1);
        jp1.add(jb1);

        JPanel jp2=new JPanel();
        jp2.add(jl2);
        jp2.add(jtf2);
        jp2.add(jcb);
        jp2.add(jb2);

        jf.add(jp1, BorderLayout.NORTH);
        jf.add(jp2);
    }

    private void setStyle(){
        jtf1.setEnabled(false);
    }
    private void addEventHandler(){
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FileDialog fd=new FileDialog(jf,"打开文件");
                fd.setVisible(true);
                if(fd.getFile()!=null){
                    jtf1.setText(fd.getDirectory()+fd.getFile());
                }
            }
        });

        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if("".equals(jtf1.getText())||"".equals(jtf2.getText())){
                    JOptionPane.showMessageDialog(jf,"信息不完整");
                    return;
                }

                if(jcb.isSelected()==true){
                    //如果文件名以.enc结尾表示加密后的文件，否则是未加密文件，加密规则文件每一个字节和密钥作异或计算
                    FileInputStream fis=null;
                    FileOutputStream fos=null;

                    try {

                        fis=new FileInputStream(jtf1.getText());
                        fos=new FileOutputStream(jtf1.getText().endsWith(".enc")
                                                    ?jtf1.getText().substring(0,jtf1.getText().lastIndexOf(".enc"))
                                                    :jtf1.getText()+".enc");

                        int b;
                        while((b=fis.read())!=-1){
                            fos.write(b^Integer.parseInt(jtf2.getText()));
                        }

                        JOptionPane.showMessageDialog(jf,"加密/解密完成");
                    } catch (NumberFormatException ex) {
                        JOptionPane.showMessageDialog(jf,"密钥只能是数字");
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(jf,"加密失败");
                    } finally {
                        if(fis!=null){
                            try {
                                fis.close();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }
                        if(fos!=null){
                            try {
                                fos.close();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }

                }else{

                    RandomAccessFile raf=null;

                    try{

                        raf=new RandomAccessFile(jtf1.getText(),"rw");

                        int b;

                        while(raf.getFilePointer()<raf.length()){
                            b=raf.read();
                            raf.seek(raf.getFilePointer()-1);
                            raf.write(b^Integer.parseInt(jtf2.getText()));
                        }

                    }catch (Exception ex){
                        JOptionPane.showMessageDialog(jf,"加密/解密失败");
                    }finally{
                        if(raf!=null){
                            try {
                                raf.close();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }



                }
            }
        });
    }

    public void show(){
        init();
        setStyle();
        addEventHandler();
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new FileEncoryptor().show();
    }
}
